package com.longcai.service.impl.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjGift;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.mapper.clyjuser.ClyjGiftMapper;
import com.longcai.mapper.goods.ClyjGoodsHomepageMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjGiftService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 礼包表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-18
 */
@Service
public class ClyjGiftServiceImpl extends ServiceImpl<ClyjGiftMapper, ClyjGift> implements ClyjGiftService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGiftMapper clyjGiftMapper;
    @Autowired
    private ClyjGoodsHomepageMapper goodsMapper;

    /**
     * 分页查询
     *
     * @param clyjGift
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse giftList(ClyjGift clyjGift, PageHelper pageHelper) {
        //分页
        Page<ClyjGift> page = new Page<ClyjGift>(pageHelper.getStart(), pageHelper.getLength());
        QueryWrapper<ClyjGift> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clyjGift.getGiftName())) {
            queryWrapper.like("gift_name", clyjGift.getGiftName());
        }
        if (!StringUtils.isEmpty(clyjGift.getZkPrice())) {
            queryWrapper.ge("zk_price", clyjGift.getZkPrice());
        }
        if (!StringUtils.isEmpty(clyjGift.getZkPrice2())) {
            queryWrapper.le("zk_price", clyjGift.getZkPrice2());
        }
        if (!StringUtils.isEmpty(clyjGift.getBuyBefor())) {
            queryWrapper.eq("buy_befor", clyjGift.getBuyBefor());
        }
        if (!StringUtils.isEmpty(clyjGift.getBuyAfter())) {
            queryWrapper.eq("buy_after", clyjGift.getBuyAfter());
        }
        queryWrapper.in("status","0","1");
        queryWrapper.in("gift_type","2","1");
        IPage<ClyjGift> clyjGiftIPage = this.clyjGiftMapper.selectPage(page, queryWrapper);
        List<ClyjGift> records = clyjGiftIPage.getRecords();
        for (ClyjGift clyjGift2 : records) {
			if(clyjGift2.getGoodsName()==null || clyjGift2.getGoodsName().equals(""))
				continue;
			//查询商品名称
			ClyjGoodsHomepage selectById = goodsMapper.selectById(clyjGift2.getGoodsName());
			clyjGift2.setGoodsName(selectById.getGoodsName());
		}
        clyjGiftIPage.setRecords(records);
        PageTableResponse pageTableResponse = new PageTableResponse(clyjGiftIPage.getTotal(),
                clyjGiftIPage.getTotal(), clyjGiftIPage.getRecords());
        return pageTableResponse;
    }

    /**
     * 新增
     *
     * @param clyjGift
     * @return
     */
    @Override
    public Integer saveGift(ClyjGift clyjGift) {
        try {
            ClyjGift clyjGift1 = clyjGift;
            clyjGift1.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjGift1.setCreateTime(new Date());
            Integer integer = this.clyjGiftMapper.insert(clyjGift1);
            logger.debug("新增成功++++++++++++", integer);
            return integer;
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 修改
     *
     * @param clyjGift
     * @return
     */
    @Override
    public Integer updategift(ClyjGift clyjGift) {

        try {

            clyjGift.setUpdateTime(new Date());
            logger.debug("查询成功");
            return this.clyjGiftMapper.updateById(clyjGift);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delGift(String id) {
        try {

            ClyjGift clyjGift = this.clyjGiftMapper.selectById(id);
            clyjGift.setStatus("2");
            this.clyjGiftMapper.updateById(clyjGift);
            logger.debug("查询成功");
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
    }
}
